//
// Created by Semir on 2020/4/26.
//

#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>

using namespace std;

const int MAX_N = 10005;
int n;
priority_queue<int, vector<int>, greater<int>> Q;

/**
 * 使用优先级队列
 * @return
 */
int main() {
    cin >> n;
    int a, b;
    for (int i = 0; i < n; i++) {
        cin >> a;
        Q.push(a);
    }
    long long cost = 0;
    while (Q.size() > 1) {
        a = Q.top();
        Q.pop();
        b = Q.top();
        Q.pop();
        a = a + b;
        cost += a;
        Q.push(a);
    }
    cout << cost << endl;
    return 0;
}